package com.iocharger.demo.controller;

import com.iocharger.demo.ServerApplication;
import com.iocharger.demo.api.IIndexService;
import com.iocharger.demo.dto.Result;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RefreshScope
@RequestMapping("/index")//此处的value要等于feignclient的path
public class IndexController implements IIndexService {
    @Value("${config.name}")
    private String userName;
    @Autowired
    private ApplicationEventPublisher publisher;
    public Result<?> index() {
        return Result.success(userName);
    }

    @GetMapping("publish")
    public Result<?> publish(String name){
        ServerApplication.TestEvent testEvent = new ServerApplication.TestEvent(this, name);
        publisher.publishEvent(testEvent);
        log.info("event has been published, event: {}", testEvent);
        return Result.success();
    }

}
